<?php

class Ead_MaterialController extends App_Controller_Action
{
    public $dir = 'public/upload/material/';
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_list.js');
    
        $search = $this->_getParam('inp-search', '');
        $this->view->search = $search;
        $where = 'MAT_ID > 0';
        if(strlen($search) > 1){
            $find = 'MAT_NOM like "%'.$where.'%"';
        }
        
        $obj = new Ead_Model_DbTable_Material();
        //$result = $obj->fetchAll($where);
        $result = $obj->fetchAll($obj->select()->where($where)->order(array('MAT_ORD ASC','MAT_NOM DESC')));
        $this->view->result = $this->paginacao($result);
    }
    
    
    public function editAction() {
        
        $this->view->headScript()->appendScript('var tipmat = 2 ;');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_geral.js');
        
        
        $form = new Ead_Form_Material();
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ckeditor/ckeditor.js');
        $option = array('width'=>'70%');
        echo $this->view->CkeditorHelper('MAT_DESC',$option);
        
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('MAT_ID');

                $data = array(
                    'MAT_ID'    => $form->getValue('MAT_ID'),
                    'MAT_NOM'   => $form->getValue('MAT_NOM'),
                    'MAT_DESC'  => $form->getValue('MAT_DESC'),
                    'CTC_ID'    => $form->getValue('CTC_ID'),
                    'REP_ID'    => $form->getValue('REP_ID'),
                    'MAT_CENE'  => $form->getValue('MAT_CENE'),
                    'MAT_STA'   => $form->getValue('MAT_STA'),
                    'MAT_ORD'   => $form->getValue('MAT_ORD'),
                    'CTC_ID'    => implode(',', $form->getValue('CTC_IDS')),
                );
                
                
                $obj = new Ead_Model_DbTable_Material();

                if ($id == 0){
                    $obj->insert($data);
                    //$id =  $obj->getAdapter()->lastInsertId();                    
                }
                else{
                    $obj->update($data, 'MAT_ID = ' . $id);;
                }

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Ead_Model_DbTable_Material();
                $row = $obj->fetchRow('MAT_ID = ' . $id);//->toArray();
                
                $rep = $row->findDependentRowSet("Ead_Model_DbTable_Repositorio"); 
                $row = $row->toArray();
                $row['REP_NOM'] = $rep[0]['REP_NOM'];
                
                $form->populate($row);
                
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $obj = new Ead_Model_DbTable_Material();
            $ok = (bool) $obj->delete('MAT_ID = '.(int)$id);
            
            Zend_Loader::loadClass('imagem','library');
            imagem::delete($id,$this->dir);
            //$this->_helper->redirector('index'); 
        } else {
            //$id = $this->_getParam('id', 0);
            //$obj = new Ead_Model_DbTable_Material();
            //$form->populate($obj->fetchRow('MAT_ID = ' . $id)->toArray());
        }
        
        echo json_encode(array('ok' => $ok));
    }
    
    
}

